package com.jintoufs.weixin.contacts;

import com.alibaba.fastjson.JSON;
import com.jintoufs.domain.address.Address;
import com.jintoufs.domain.contacts.Contacts;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.service.address.AddressService;
import com.jintoufs.service.contacts.ContactsService;
import com.jintoufs.util.valid.ValidUtil;
import com.jintoufs.weixin.SupportAction;
import io.swagger.annotations.Api;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "联系人相关接口", description = "联系人相关接口")
@RestController
@RequestMapping("/api/contacts")
public class ContactsAction extends SupportAction {
    @Resource
    private ContactsService contactsService;

    @GetMapping("/oper")
    public AjaxMsg oper(String contacts, String token, String type) {
        Contacts en = JSON.parseObject(contacts, Contacts.class);
        AjaxMsg ajaxMsg = new AjaxMsg();
        Customer customer = getCustomerByToken(token);
        if (ValidUtil.isEmpty(customer)) {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("Token验证失败");
        } else {

            switch (type) {
                case "add":
                    Map<String, Object> param = new HashMap<>();
                    param.put("userId", customer.getId());
                    param.put("isDefault", true);
                    List<Contacts> contactsList = contactsService.selectByParam(param);
                    if (contactsList.size() == 0) {
                        en.setIsDefault(true);
                    } else {
                        if (en.getIsDefault()) {
                            contactsList.get(0).setIsDefault(false);
                            contactsService.save(contactsList.get(0), null);
                        }
                    }
                    en.setUserId(customer.getId());
                    contactsService.save(en, getUUID());
                    break;
                case "edit":
                    Map<String, Object> param2 = new HashMap<>();
                    param2.put("userId", customer.getId());
                    param2.put("isDefault", true);
                    List<Contacts> contactsList2 = contactsService.selectByParam(param2);
                    if (contactsList2.size() == 0) {
                        en.setIsDefault(true);
                    } else {
                        if (en.getIsDefault()) {
                            contactsList2.get(0).setIsDefault(false);
                            contactsService.save(contactsList2.get(0), null);
                        }
                    }
                    en.setUserId(customer.getId());
                    contactsService.save(en, null);
                    break;
                case "del":
                    contactsService.delete(en.getId());
                    break;
                default:
                    ajaxMsg.setCode(AjaxMsg.FAILURE);
                    ajaxMsg.setMessage("未知操作。");
                    break;
            }
        }
        return ajaxMsg;
    }


    @GetMapping("/search")
    public AjaxMsg search(String token) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        Customer customer = getCustomerByToken(token);
        if (ValidUtil.isEmpty(customer)) {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("Token验证失败");
        } else {
            Map<String, Object> param = new HashMap<>();
            param.put("userId", customer.getId());
            try {
                ajaxMsg.setDatas(contactsService.selectByParam(param));
            } catch (Exception e) {
                e.printStackTrace();
                ajaxMsg.setCode(AjaxMsg.FAILURE);
                ajaxMsg.setMessage("传入参数错误");
            }
        }
        return ajaxMsg;
    }
}
